﻿using Abp.Domain.Repositories;
using Abp.EntityFrameworkCore.EFPlus;
using Abp.Extensions;
using Mt.Site.Core.WorkFlows.Enum;
using Mt.Site.Core.WorkFlows.Process.Entities;
using Mt.Site.Core.WorkFlows.Process.Models;
using Mt.Site.Core.WorkFlows.Process.Stores;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Mt.Site.Core.WorkFlows.Process
{
    public static class WorkFlowStaticFuncCollection
    {
        public static bool CheckIsMainOperator(this WorkFlowProcessDto current)
        {
            if(current == null)
            {
                return false;
            }
            if (current.Signal == WorkFlowConst.Constant.ZhuBan)
            {
                return true;
            }
            return false;
        }

        public static bool CheckIsMainOperatorSubmitLeader(this WorkFlowProcessDto current)
        {
            if (current == null)
            {
                return false;
            }
            if (current.Signal == WorkFlowConst.Constant.ZhuBanLeader)
            {
                if(current.Signal2 == "true")
                {
                    return true;
                }
            }
            return false;
        }


        public static bool CheckIsFeedBackForLeader(this WorkFlowProcessDto current)
        {
            if (current == null)
            {
                return false;
            }
            if (current.Signal == WorkFlowConst.Constant.FeedbackForLeader)
            {
                return true;
            }
            return false;
        }

        public static bool CheckIsCoOperator(this WorkFlowProcessDto current)
        {
            if (current == null)
            {
                return false;
            }
            if (current.Signal == WorkFlowConst.Constant.HuiBan)
            {
                return true;
            }
            return false;
        }

        internal static void GetResponsibilityData(
            List<IResponsibilityLink> allData, IResponsibilityLink task, List<long> toDoUserIds, List<WorkFlowRelationshipDto> workFlowRelationships)
        {
            //  催办 的责任链查找，同时存在下面两种情况
            // 场景1(多对一)： 如会办人员A提交领导B审阅，在B还未提交时，会办人员C,D也提交给B审阅，于是C,D通过workFlowRelationships与B关联，
            //  A则通过B.ResponsibilityId与之关联， 通过B 查找A,C,D
            /*
             * 
                场景2(一对多)： A转办给B,C,B转办给D, 通过D查找A，D.ResponsibilityId找到B的办结任务，B.ResponsibilityId找到A的办结任务
             */
            var responsibilityIdList = workFlowRelationships.Where(c => c.TaskId == task.Id).Select(c => c.ResponsibilityId).ToArray();
            if (responsibilityIdList.Length > 0)
            {
                var responsibilityUser = allData.Where(c => responsibilityIdList.Contains(c.Id)).Select(c => c.UserId);
                if (responsibilityUser.Count() > 0)
                {
                    toDoUserIds.AddRange(responsibilityUser);
                }
            }

            if (task.ResponsibilityId == null)
            {
                toDoUserIds.Add(task.UserId); //主办或会办人员自己
            }
            else if(task.ResponsibilityId != Guid.Empty)
            {
                Guid? responsibilityId = task.ResponsibilityId;
                while (responsibilityId != null)
                {
                    task = allData.FirstOrDefault(c => c.Id == task.ResponsibilityId.Value);
                    if (task == null)    //数据有误 ，退出查找
                    {
                        responsibilityId = null;
                    }
                    else
                    {
                        responsibilityId = task.ResponsibilityId;
                        if (responsibilityId == null)
                        {
                            toDoUserIds.Add(task.UserId);  //找到主办或会办
                        }

                        //主办、会办转给副部长，再转给部室成员，就会需要递归查找
                        responsibilityIdList = workFlowRelationships.Where(c => c.TaskId == task.Id).Select(c => c.ResponsibilityId).ToArray();
                        if (responsibilityIdList.Length > 0)
                        {
                            var responsibilityUser = allData.Where(c => responsibilityIdList.Contains(c.Id)).Select(c => c.UserId);
                            if (responsibilityUser.Count() > 0)
                            {
                                toDoUserIds.AddRange(responsibilityUser);
                            }
                        }
                    }
                }
            }
        }

        internal static List<long> GetResponsibilityDataNoRecursive(
            List<IResponsibilityLink> allData, IResponsibilityLink task, List<WorkFlowRelationshipDto> workFlowRelationships)
        {
            //1. 领导意见处理 同样是 多对一。但这里的领导可能是审批领导，如果顺着责任链递归查找，就找到主办或会办人员了
            //2. 主办、会办的领导查找对应的主办、会办，关系链只有一层，无需递归。

            var toDoUserIds = new List<long>();

            var responsibilityIdList = workFlowRelationships.Where(c => c.TaskId == task.Id).Select(c => c.ResponsibilityId).ToArray();
            if (responsibilityIdList.Length > 0)
            {
                var responsibilityUser = allData.Where(c => responsibilityIdList.Contains(c.Id)).Select(c => c.UserId);
                if (responsibilityUser.Count() > 0)
                {
                    toDoUserIds.AddRange(responsibilityUser);
                }
            }

            if (task.ResponsibilityId != Guid.Empty && task.ResponsibilityId != null)
            {
                var task2 = allData.FirstOrDefault(c => c.Id == task.ResponsibilityId.Value);
                if (task2 != null)
                {
                    toDoUserIds.Add(task2.UserId);
                }
            }

            return toDoUserIds;
        }

        /// <summary>
        /// 多对1 的 场景下 ， 由1查找对应的多(如： 会办领导 查找他审批的会办人员)
        /// </summary>
        /// <param name="allData"></param>
        /// <param name="task"></param>
        /// <param name="preTaskList"></param>
        /// <param name="workFlowRelationships"></param>
        internal static List<WorkFlowProcessDto> GetResponsibilityDataNoRecursive(
             List<WorkFlowProcessDto> allData, WorkFlowProcessDto task, List<WorkFlowRelationshipDto> workFlowRelationships)
        {
            List<WorkFlowProcessDto> preTaskList = new List<WorkFlowProcessDto>();

            var responsibilityIdList = workFlowRelationships.Where(c => c.TaskId == task.Id).Select(c => c.ResponsibilityId).ToArray();
            if (responsibilityIdList.Length > 0)
            {
                var responsibilityUser = allData.Where(c => responsibilityIdList.Contains(c.Id));
                if (responsibilityUser.Count() > 0)
                {
                    preTaskList.AddRange(responsibilityUser);
                }
            }

            if (task.ResponsibilityId != Guid.Empty && task.ResponsibilityId != null)
            {
                var task2 = allData.FirstOrDefault(c => c.Id == task.ResponsibilityId.Value);
                if (task2 != null)
                {
                    preTaskList.Add(task2);
                }
            }
            return preTaskList;
        }

        /// <summary>
        /// 转移会办领导与会办人员直接的关系
        /// </summary>
        /// <param name="item"></param>
        /// <param name="toPreStepTask"></param>
        internal static WorkFlowRelationshipDto MoveLeaderTask(WorkFlowProcessDto item, WorkFlowProcessDto toPreStepTask
            , List<WorkFlowRelationshipDto> workFlowRelationships)
        {
            //
            item.PreStepId = toPreStepTask.Id;
            item.PreOperatorId = toPreStepTask.Id;
            item.ResponsibilityId = toPreStepTask.Id;
            //item.Code;

            //移除旧关系
            return workFlowRelationships.FirstOrDefault(c => c.TaskId == item.Id && c.ResponsibilityId == toPreStepTask.Id);
        }

    }
}
